package com.ruoyi.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.UncheckDraftMapper;
import com.ruoyi.mapper.ArticleUncheckMapper;
import com.ruoyi.mapper.ConlumnMapper;
import com.ruoyi.domain.UncheckDraft;
import com.ruoyi.domain.ArticleUncheck;
import com.ruoyi.domain.Conlumn;
import com.ruoyi.service.IUncheckDraftService;

/**
 * 未审核文章草稿箱Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-14
 */
@Service
public class UncheckDraftServiceImpl extends ServiceImpl<UncheckDraftMapper, UncheckDraft> implements IUncheckDraftService
{
    @Autowired
    private UncheckDraftMapper uncheckDraftMapper;

    @Autowired
    private ArticleUncheckMapper articleUncheckMapper;

    @Autowired
    private ConlumnMapper conlumnMapper;

    /**
     * 查询未审核文章草稿箱
     *
     * @param articleId 未审核文章草稿箱ID
     * @return 未审核文章草稿箱
     */
    @Override
    public UncheckDraft selectUncheckDraftByArticleId(Long articleId)
    {
        return uncheckDraftMapper.selectUncheckDraftByArticleId(articleId);
    }

    /**
     * 查询未审核文章草稿箱列表
     *
     * @param uncheckDraft 未审核文章草稿箱
     * @return 未审核文章草稿箱
     */
    @Override
    public List<UncheckDraft> selectUncheckDraftList(UncheckDraft uncheckDraft)
    {
        return uncheckDraftMapper.selectUncheckDraftList(uncheckDraft);
    }

    /**
     * 新增未审核文章草稿箱
     *
     * @param uncheckDraft 未审核文章草稿箱
     * @return 结果
     */
    @Override
    public int insertUncheckDraft(UncheckDraft uncheckDraft)
    {
        return uncheckDraftMapper.insertUncheckDraft(uncheckDraft);
    }

    /**
     * 修改未审核文章草稿箱
     *
     * @param uncheckDraft 未审核文章草稿箱
     * @return 结果
     */
    @Override
    public int updateUncheckDraft(UncheckDraft uncheckDraft)
    {
        return uncheckDraftMapper.updateUncheckDraft(uncheckDraft);
    }

    /**
     * 批量删除未审核文章草稿箱
     *
     * @param articleIds 需要删除的未审核文章草稿箱ID
     * @return 结果
     */
    @Override
    public int deleteUncheckDraftByArticleIds(Long[] articleIds)
    {
        return uncheckDraftMapper.deleteUncheckDraftByArticleIds(articleIds);
    }

    /**
     * 删除未审核文章草稿箱信息
     *
     * @param articleId 未审核文章草稿箱ID
     * @return 结果
     */
    @Override
    public int deleteUncheckDraftByArticleId(Long articleId)
    {
        return uncheckDraftMapper.deleteUncheckDraftByArticleId(articleId);
    }

    /**
     * 检查是否为媒体聚焦栏目
     *
     * @param columnId 栏目ID
     * @return 是否为媒体聚焦栏目
     */
    private boolean isMediaFocusColumn(Long columnId) {
        if (columnId == null) {
            return false;
        }

        // 查询栏目信息
        Conlumn column = conlumnMapper.selectConlumnById(columnId);
        if (column == null) {
            return false;
        }

        // 判断栏目名称是否为"媒体聚焦"
        return "媒体聚焦".equals(column.getName());
    }

    /**
     * 发布未审核文章草稿箱
     *
     * @param articleId 未审核文章草稿箱ID
     * @return 结果
     */
    @Override
    public int publishUncheckDraft(Long articleId)
    {
        // 先将草稿文章插入到正式文章表中
        UncheckDraft draft = uncheckDraftMapper.selectUncheckDraftByArticleId(articleId);
        if (draft == null) {
            return 0; // 草稿不存在
        }

        // 检查是否上传了审批表
        if (draft.getAppval() == null || draft.getAppval().isEmpty()) {
            throw new RuntimeException("请上传审批表");
        }

        // 检查媒体聚焦栏目是否上传了封面图片
        if (draft.getColumnId() != null && isMediaFocusColumn(draft.getColumnId())) {
            // 检查封面图片是否上传
            if (draft.getCoverPath() == null || draft.getCoverPath().isEmpty()) {
                throw new RuntimeException("媒体聚焦栏目必须上传封面图片");
            }
            // 注意：这里无法直接检查已上传图片的大小，因为图片已存储在OSS中
            // 如果需要检查大小，需要在上传时就进行验证
        }

        // 将UncheckDraft转换为ArticleUncheck
        ArticleUncheck article = new ArticleUncheck();
        article.setTitle(draft.getTitle());
        article.setArticleOrigin(draft.getArticleOrigin());
        article.setColumnId(draft.getColumnId());
        article.setContent(draft.getContent());
        article.setAuthor(draft.getAuthor());
        article.setCoverPath(draft.getCoverPath());
        article.setIp(draft.getIp());
        article.setAppval(draft.getAppval());
        article.setLittleTitle1(draft.getLittleTitle1());
        article.setLittleTitle2(draft.getLittleTitle2());
        article.setState("0"); // 未审核状态
        article.setCreateTime(draft.getCreateTime());
        article.setCreateBy(draft.getCreateBy());
        article.setUpdateTime(new Date()); // 更新时间为当前时间
        article.setUpdateBy(draft.getUpdateBy());
        article.setRemark(draft.getRemark());
        article.setAttachment(draft.getAttachment());
        // 不设置发布日期，发布日期应该在审核时设置

        // 插入到正式文章表中
        int result = articleUncheckMapper.insertArticleUncheck(article);
        if (result > 0) {
            // 删除草稿文章
            uncheckDraftMapper.deleteUncheckDraftByArticleId(articleId);
        }
        return result;
    }
}